local jiuquxinfu = fk.CreateSkill{
  name = "hanqing__jiuquxinfu",
}

Fk:loadTranslationTable{
  ["hanqing__jiuquxinfu"] = "旧曲新福",
  [":hanqing__jiuquxinfu"] = "出牌阶段限一次，你可以将一张牌当<a href=':hanqing__favorable_auspices'>【大吉大利】</a>使用，"..
  "你可以交给一名目标一张牌并令其改为展示此牌。若展示牌中红色更多，此牌结算后你视为使用<a href=':bogus_flower'>【树上开花】</a>。",

  ["#hanqing__jiuquxinfu"] = "旧曲新福：将一张牌当【大吉大利】使用",
  ["#hanqing__jiuquxinfu-give"] = "旧曲新福：你可以交给一名目标一张牌，其改为展示此牌",

  ["$hanqing__jiuquxinfu1"] = "我恭喜你发财，我恭喜你精彩。",
  ["$hanqing__jiuquxinfu2"] = "最好的请过来，不好的请走开。",
}

jiuquxinfu:addEffect("viewas", {
  anim_type = "support",
  mute_card = false,
  prompt = "#hanqing__jiuquxinfu",
  handly_pile = true,
  card_filter = function(self, player, to_select, selected)
    return #selected == 0
  end,
  view_as = function(self, player, cards)
    if #cards ~= 1 then return end
    local card = Fk:cloneCard("hanqing__favorable_auspices")
    card.skillName = jiuquxinfu.name
    card:addSubcard(cards[1])
    return card
  end,
  after_use = function (self, player, use)
    local room = player.room
    if not player.dead then
      local info = (use.extra_data or {}).hanqing__favorable_auspices
      if info then
        local red, black = 0, 0
        for _, dat in ipairs(info) do
          if Fk:getCardById(dat[2]).color == Card.Red then
            red = red + 1
          else
            black = black + 1
          end
        end
        if red > black then
          room:useVirtualCard("bogus_flower", nil, player, player, jiuquxinfu.name)
        end
      end
    end
  end,
  enabled_at_play = function(self, player)
    return player:usedSkillTimes(jiuquxinfu.name, Player.HistoryPhase) == 0
  end,
})

jiuquxinfu:addEffect(fk.AfterCardUseDeclared, {
  mute = true,
  is_delay_effect = true,
  can_trigger = function (self, event, target, player, data)
    return target == player and
      table.contains(data.card.skillNames, jiuquxinfu.name) and data.card.name == "hanqing__favorable_auspices" and
      not player:isNude() and table.find(data.tos, function (p)
        return not p.dead
      end)
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local to, card = room:askToChooseCardsAndPlayers(player, {
      min_card_num = 1,
      max_card_num = 1,
      min_num = 1,
      max_num = 1,
      targets = data.tos,
      skill_name = jiuquxinfu.name,
      pattern = ".|.|.|hand",
      prompt = "#hanqing__jiuquxinfu-give",
      cancelable = true,
    })
    if #to > 0 and #card > 0 then
      event:setCostData(self, {tos = to, cards = card})
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local to = event:getCostData(self).tos[1]
    local card = event:getCostData(self).cards[1]
    if not table.contains(to:getCardIds("h"), card) then
      room:moveCardTo(card, Card.PlayerHand, to, fk.ReasonGive, jiuquxinfu.name, nil, false, player)
    end
    data.extra_data = data.extra_data or {}
    data.extra_data.hanqing__jiuquxinfu = {to.id, card}
  end
})

jiuquxinfu:addEffect(fk.CardEffectFinished, {
  can_refresh = function (self, event, target, player, data)
    return table.contains(data.card.skillNames, jiuquxinfu.name) and data.card.name == "hanqing__favorable_auspices" and
      data.to == player and data.extra_data and data.extra_data.hanqing__jiuquxinfu and
      data.extra_data.hanqing__jiuquxinfu[1] == player.id
  end,
  on_refresh = function (self, event, target, player, data)
    local info = data.extra_data.hanqing__favorable_auspices
    if info then
      for _, dat in ipairs(info) do
        if dat[1] == player.id then
          dat[2] = data.extra_data.hanqing__jiuquxinfu[2]
          break
        end
      end
    end
    data.extra_data.hanqing__favorable_auspices = info
  end,
})

jiuquxinfu:addAI(nil, "vs_skill")

return jiuquxinfu
